
L.CheckboxTool = L.Tool.extend({

  initialize: function(correctness){
    this._correctness = this._correctness=='good'?'good':'bad';
    this.name = "checkbox_tool_"+correctness;

    if(correctness == "good"){
      var good = "";
      good += "M256,50C142.229,50,50,142.229,50,256s92.229,206,206,206s206-92.229,206-206S369.771,50,256,50z";
      good += "M220.953,366.959l-97.326-97.352l41.44-41.448l55.884,55.899l129.018-129.018l41.459,41.431L220.953,366.959z";
      good += "M220.953,366.959l-97.326-97.352l41.44-41.448l55.884,55.899l129.018-129.018l41.459,41.431L220.953,366.959z";
      this._icon = good;
      this._color = "#0ff801";
    }else{
      var bad = "";
      bad += "M256,50C142.229,50,50,142.229,50,256s92.229,206,206,206s206-92.229,206-206S369.771,50,256,50z"
      bad += "M334.124,378.545l-77.122-77.117l-77.123,77.127l-41.425-41.449l77.106-77.117l-77.115-77.11l41.448-41.424l77.103,77.092"
      bad += "l77.09-77.102l41.459,41.432l-77.104,77.108l77.113,77.102L334.124,378.545z"
      bad += "M334.124,378.545l-77.122-77.117l-77.123,77.127l-41.425-41.449l77.106-77.117l-77.115-77.11l41.448-41.424l77.103,77.092"
      bad += "l77.09-77.102l41.459,41.432l-77.104,77.108l77.113,77.102L334.124,378.545z"
      this._icon = bad;
      this._color = "#f00";
    }

  },

  enable: function(editor){
    console.log('editor');console.log(editor);
    L.Tool.prototype.enable.call(this, editor);

    // Drawing state
    this._drawing = false;

    // para no tapar la figura con el dedo en pantalla touch
    this._fix_rect_x = 40;
    this._fix_rect_y = 40;
    this._fix_path_x = 275;
    this._fix_path_y = 275;
  },

  onDragstartElement: function(element, event){},
  onDragmoveElement: function(element, event){},
  onDragendElement: function(element, event){},

  onDragstartCanvas: function(canvas, event){
    var pos = canvas.getPos(event);
    var x = pos.x;
    var y = pos.y;
    
    if(x >= 10 && y >= 10 && this._drawing == false){
      this._drawing = true;
      document.activeElement.blur();
      var tx = this._tx = x - this._fix_path_x;
      var ty  = this._ty = y - this._fix_path_y;

      this._path = canvas.getRaphael().path(this._icon)
                  .attr({fill: this._color,stroke: "#000", "stroke-width":4})
                  .transform("t" + tx + "," + ty + "s0.11");
      //this._rect = canvas.getRaphael().rect().attr({x:x - this._correctionX, y:y - this._correctionY,
      //                            width:46, height:46, fill:"#f00", stroke:"#f00"});
    }
  },

  onDragmoveCanvas: function(canvas, event){
    if (this._drawing == true) {
      var pos = canvas.getPos(event);
      var x = pos.x;
      var y = pos.y;
      var tx = this._tx = x - this._fix_path_x;
      var ty = this._ty = y - this._fix_path_y;
                        
      this._path.transform("t" + tx + "," + ty + "S0.11");
      //this._rect.attr({x:x - this._correctionX,y:y - this._correctionX})
    }
  },

  onDragendCanvas: function(canvas, event){
    if (this._drawing == true) {
      var self = this;
      var pos = canvas.getPos(event);
      var x = pos.x;
      var y = pos.y;
      var tx = this._tx;
      var ty = this._ty;

      setTimeout(function(){
        //alert("1)tx:ty->"+tx+":"+ty);
        //var attrs = { "correctness":self._correctness,
        //              "tx":tx, "ty":ty};

        var attrs = self._path.attr();
        attrs.type = "path"; //rapahel type
        attrs.logical_type = "checkbox_"+ self._correctness;
        self._path.remove();
        console.log('attrs');console.log(attrs);
        //this._rect.remove();
        canvas.addElement(new L.Element(attrs));

        var selectT = new L.SelectTool(self._editor);
        self._editor.tool(selectT);
      }, 0);
      this._drawing = false;
    }
  },

  disable: function(editor){
    document.activeElement.blur();
  },

});